
import store from '@/store/index.js'

export const storage = {

	//token是否存在
	isLogin(){
		let token = this.getStorageDataSync(store.state.token)
		if(token){
			return true
		}else{
			return false
		}
	},
	//同步缓存数据
	setStorageDataSync(key, data){
		try {
		    uni.setStorageSync(key, JSON.stringify(data));
		} catch (e) {
			console.log("error:",e)
		}
	},
	//同步获取缓存数据
	getStorageDataSync(key){
		try {
		    const data = uni.getStorageSync(key);
		    if (data) {
				return JSON.parse(data)
		    }
		} catch (e) {
		    console.log("error:",e)
		}
	},
	//同步清除缓存数据
	removeStorageDataSync(key){
		try {
		    uni.removeStorageSync(key);
		} catch (e) {
		    console.log("error:",e)
		}
	},
	//异步缓存数据
	setStorageData(key, data){
		uni.setStorage({
		    key: key,
		    data: JSON.stringify(data),
		    success: function () {
		        // console.log('success');
		    },
			fail: function () {
				// console.log('error');
			},
			complete: function () {
				// console.log('complete');
			}
		});
	},
	//异步获取缓存数据
	getStorageData(key){
		return new Promise((resolve, reject)=>{
			uni.getStorage({
			    key: key,
			    success: function (res) {
					resolve(JSON.parse(res.data));
			    },
				fail: function () {
					// console.log('error');
				},
				complete: function () {
					// console.log('complete');
				}
			});
		})
	},
}
